package com.syni.mdd.sjb.community.base.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @className MybatisUtils
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/19 20:48
 * @VERSION 1.0
 **/
public class MybatisUtils {
    private static final String MIN_DATE_STR = "0001-01-01";
    private static final String MAX_DATE_STR = "9999-12-31";


    public static Map<String, Object> UserData(IPage iPage) {
        Map<String, Object> userData = new HashMap<>();
        userData.put("count", iPage.getTotal());
        userData.put("pageNum", iPage.getCurrent());
        userData.put("pageSize", iPage.getSize());
        return userData;
    }

    public static Map<String, Object> UserData(Map<String, Object> userData, IPage iPage) {
        userData.put("count", iPage.getTotal());
        userData.put("pageNum", iPage.getCurrent());
        userData.put("pageSize", iPage.getSize());
        return userData;
    }

    public static Map<String, Object> UserData(Integer pageNum, Integer pageSize, Integer count) {
        Map<String, Object> userData = new HashMap<>();
        userData.put("count", count);
        userData.put("pageNum", pageNum);
        userData.put("pageSize", pageSize);
        return userData;
    }

    /**
     * 获取开始日期条件
     *
     * @param beginDay 开始日期字符串
     * @return 日期
     */
    public static LocalDate getBeginDay(String beginDay) {
        if (StringUtils.isNotBlank(beginDay)) {
            return LocalDate.parse(beginDay, DateTimeFormatter.ISO_DATE);
        } else {
            return LocalDate.parse(MIN_DATE_STR, DateTimeFormatter.ISO_DATE);
        }
    }

    /**
     * 获取结束日期条件
     *
     * @param endDay 结束日期字符串
     * @return 日期
     */
    public static LocalDate getEndDay(String endDay) {
        if (StringUtils.isNotBlank(endDay)) {
            return LocalDate.parse(endDay, DateTimeFormatter.ISO_DATE);
        } else {
            return LocalDate.parse(MAX_DATE_STR, DateTimeFormatter.ISO_DATE);
        }
    }
}
